package com.ning.farm_income_back.controller;

import com.github.pagehelper.PageInfo;
import com.ning.farm_income_back.domain.Costs;
import com.ning.farm_income_back.dto.AgriculturalassetsDto;
import com.ning.farm_income_back.dto.CostsDto;
import com.ning.farm_income_back.service.CostsService;
import com.ning.farm_income_back.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/costs")
public class CostsController {
    @Autowired
    private CostsService costsService;

    //分页带条件查询
    @PostMapping("/page")
    public ResultVo<PageInfo> page(@RequestBody CostsDto costDto, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize ) {
        if(pageNum == null || pageNum < 1) pageNum = 1;

        //获取页面数据 总条数 总页数
        PageInfo pageInfo = costsService.getPage(costDto,pageNum,pageSize);

        return ResultVo.success("页面数据查询成功",pageInfo);
    }

    //新增
    @PostMapping("/saveCosts")
    public ResultVo saveCost(@RequestBody CostsDto costsDto,@RequestParam String username) {
        costsService.saveCostInfo(costsDto,username);
        return ResultVo.success("新增成功！");
    }

    @PutMapping("/updateCost")
    public ResultVo updateCost(@RequestBody CostsDto costsDto,@RequestParam String username) {
        costsService.updateCostInfo(costsDto,username);
        return ResultVo.success("修改成功！");
    }

    //删除
    @DeleteMapping("/deleteCost/{id}")
    public ResultVo deleteCost(@PathVariable Integer id){
        costsService.deleteCostInfo(id);
        return ResultVo.success("删除成功");
    }

    //获取年份区间的成本数组
    @GetMapping("/getCostsList")
    public ResultVo<List<String>> getCostsList(@RequestParam String startY,@RequestParam String endY){
        List<String> res = costsService.getCostsEveryYear(startY,endY);
        return ResultVo.success("获取成功",res);
    }

    //更新审核状态
    @PutMapping("/updateCostStatus")
    public ResultVo updateCostStatus(@RequestBody Costs costs) {
        costsService.updateCostStatus(costs);
        return ResultVo.success("更新成功！");
    }

    //审核驳回原因
    @PutMapping("/rejectCost")
    public ResultVo rejectCost(@RequestBody Costs costs) {
        costsService.rejectCost(costs);
        return ResultVo.success("更新成功！");
    }

}
